package com.iespring.service.admin.keyword;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.keyword.bo.KeywordCreateBO;
import com.iespring.bean.admin.keyword.pojo.Keyword;
import com.iespring.bean.admin.keyword.pojo.KeywordExample;
import com.iespring.bean.admin.keyword.vo.KeywordCreateVO;
import com.iespring.bean.admin.keyword.vo.ListKeywordVO;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.mapper.admin.PromotionKeywordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author: 浩澄
 * @Date: 2022/12/29-15:07
 * @Mission: #
 */
@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    PromotionKeywordMapper keywordMapper;

    @Override
    public ListKeywordVO list(BasePageInfo info, String keyword, String url) {

        PageHelper.startPage(info.getPage(), info.getLimit());

        KeywordExample example = new KeywordExample();

        KeywordExample.Criteria criteria = example.createCriteria();

        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }

        if (url != null && !"".equals(url)) {
            criteria.andUrlLike("%" + url + "%");
        }

        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        List<Keyword> keywordList = keywordMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(keywordList);

        ListKeywordVO listKeywordVO = new ListKeywordVO();

        listKeywordVO.setTotal(pageInfo.getTotal());
        listKeywordVO.setPages(pageInfo.getPages());
        listKeywordVO.setLimit(pageInfo.getPageSize());
        listKeywordVO.setPage(pageInfo.getPageNum());
        listKeywordVO.setList(keywordList);

        return listKeywordVO;
    }

    @Override
    public KeywordCreateVO create(KeywordCreateBO keywordCreateBO) {

        Keyword keyword = new Keyword();

        keyword.setKeyword(keywordCreateBO.getKeyword());
        keyword.setUrl(keywordCreateBO.getUrl());
        keyword.setIsHot(keywordCreateBO.getIsHot());
        keyword.setIsDefault(keywordCreateBO.getIsDefault());
        keyword.setAddTime(new Date());
        keyword.setUpdateTime(new Date());
        keyword.setDeleted(false);

        Random random = new Random();
        int i = random.nextInt(100);
        keyword.setSortOrder(i);

        int row = keywordMapper.insertSelective(keyword);

        if (row == 1) {
            KeywordCreateVO keywordCreateVO = new KeywordCreateVO();

            keywordCreateVO.setId(keyword.getId());
            keywordCreateVO.setKeyword(keyword.getKeyword());
            keywordCreateVO.setUrl(keyword.getUrl());
            keywordCreateVO.setIsHot(keyword.getIsHot());
            keywordCreateVO.setIsDefault(keyword.getIsDefault());
            keywordCreateVO.setAddTime(keyword.getAddTime());
            keywordCreateVO.setUpdateTime(keyword.getUpdateTime());

            return keywordCreateVO;
        } else {
            return null;
        }
    }

    @Override
    public Keyword update(Keyword keyword) {

        Keyword keywordUpdate = new Keyword();

        keywordUpdate.setId(keyword.getId());
        keywordUpdate.setKeyword(keyword.getKeyword());
        keywordUpdate.setUrl(keyword.getUrl());
        keywordUpdate.setIsHot(keyword.getIsHot());
        keywordUpdate.setIsDefault(keyword.getIsDefault());
        keywordUpdate.setSortOrder(keyword.getSortOrder());
        keywordUpdate.setAddTime(keyword.getAddTime());
        keywordUpdate.setUpdateTime(new Date());
        keywordUpdate.setDeleted(keyword.getDeleted());

        int row = keywordMapper.updateByPrimaryKeySelective(keywordUpdate);

        return keywordUpdate;
    }

    @Override
    public int delete(KeywordCreateVO keywordCreateVO) {

        Keyword keyword = new Keyword();

        keyword.setId(keywordCreateVO.getId());
        keyword.setKeyword(keywordCreateVO.getKeyword());
        keyword.setUrl(keywordCreateVO.getUrl());
        keyword.setIsHot(keywordCreateVO.getIsHot());
        keyword.setIsDefault(keywordCreateVO.getIsDefault());
        keyword.setAddTime(keywordCreateVO.getAddTime());
        keyword.setUpdateTime(new Date());
        keyword.setDeleted(true);
        keyword.setSortOrder(0);

        int row = keywordMapper.updateByPrimaryKeySelective(keyword);

        if (row == 1){
            return 200;
        }else{
            return 621;
        }
    }

}
